package work.coffee.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.text.DecimalFormat;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

import work.coffee.dto.Coffee;

public class CoffeeDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JLabel label;
	private JScrollPane scrollPane;
	private JTable table;
	private String obj;
	private String message;
	private ArrayList<Coffee> cf;
	public CoffeeDialog(String obj,ArrayList<Coffee> cf) {
		this.obj = obj;
		this.cf = cf;
		init();
		tableCellAlignAndWidth();	
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setVisible(true);
	}
	private void init() {
		setBounds(100, 100, 700, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setLayout(new FlowLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		label = new JLabel();
		if(obj.equals("btnPrn1")){
			message="판매금액순위";
		}
		else if(obj.equals("btnPrn2")){
			message="마진액순위";
		}
		else{
			message="정보없음";
		}
		label.setText(message);
		System.out.println(obj);
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setFont(new Font("돋움", Font.BOLD, 20));
		add(label, BorderLayout.NORTH);
		scrollPane = new JScrollPane();
		add(scrollPane);
		String[] colNames = {"순위","제품코드","제품명","제품단가","판매수량","공급가액","부가세액","판매금액","마진율","마진액"};
		TableModel model = new DefaultTableModel(colNames, 0){
			@Override
			public Class<?> getColumnClass(int columnIndex) {
				return getValueAt(0, columnIndex).getClass();
			}
		};
		table = new JTable(model);
		table.setAutoCreateRowSorter(true);//탭정렬
		inputTable();
		scrollPane.setViewportView(table);
	}
	private void tableCellAlignAndWidth() {
		DefaultTableCellRenderer dtcrRight = new DefaultTableCellRenderer();
		dtcrRight.setHorizontalAlignment(SwingConstants.RIGHT);
		TableColumnModel cModel = table.getColumnModel();
		int columnCount = cModel.getColumnCount();
		cModel.getColumn(0).setPreferredWidth(3);
		cModel.getColumn(1).setPreferredWidth(50);
		cModel.getColumn(2).setPreferredWidth(100);
		cModel.getColumn(3).setPreferredWidth(50);
		cModel.getColumn(4).setPreferredWidth(50);
		cModel.getColumn(5).setPreferredWidth(50);
		cModel.getColumn(6).setPreferredWidth(50);
		cModel.getColumn(7).setPreferredWidth(50);
		cModel.getColumn(8).setPreferredWidth(5);
		cModel.getColumn(9).setPreferredWidth(50);
		
		for (int i = 0; i < columnCount; i++) {
			cModel.getColumn(i).setCellRenderer(dtcrRight);
		}
	}
	private void inputTable() {
		DecimalFormat df = new DecimalFormat("#,###");//금액 세자리마다 쉼표 찍기
		DefaultTableModel model = (DefaultTableModel)table.getModel();
		Object[] arr = new Object[10];
		int sumProvision = 0;
		int sumSurTax = 0;		
		int sumSellingPrice = 0;
		int sumMarginRate=0;
		for(int i =0;i<cf.size();i++){
			Coffee c=cf.get(i);
			System.out.println(c);
			arr[0] = i+1;//순번
			arr[1] = c.getcNum();//제품코드
			arr[2] = c.getcName();//제품명
			arr[3] = df.format(c.getcPrice());//제품단가
			arr[4] = c.getcCount();//판매수량
			sumProvision+=c.provision();
			arr[5] = df.format(c.provision());//공급가액
			sumSurTax+=c.surTax();
			arr[6] = df.format(c.surTax());//부가세액
			sumSellingPrice+=c.sellingPrice();
			arr[7] = df.format(c.sellingPrice());//판매액
			arr[8] = c.getcMargin()+"%";//마진율
			sumMarginRate+=c.marginRate();
			arr[9] = df.format(c.marginRate());//마진액	
			
			model.addRow(arr);
		}
		String[] arrSum ={"합계",null,null,null,null,df.format(sumProvision)+"",df.format(sumSurTax)+"",df.format(sumSellingPrice)+"",null,df.format(sumMarginRate)+"",null};
		
		model.addRow(arrSum);
		revalidate();
	}		
}
